<template>
	<view class="order">
		<u-sticky bgColor="#fff">
			<u-tabs :list="orderTabs" :current="currentTab" @click="handleChangeTab"></u-tabs>
		</u-sticky>
		<view class="items" v-if="list.length">
			<view class="item" v-for="(item, index) in list" :key="index" @click="toOrderDetail(item.id)">
				<view class="title">
					{{item.eventName}}
				</view>
				<view class="text">
					研学时间：{{item.time}}
				</view>
				<view class="text">
					报道时间：{{item.signTime}}
				</view>
				<view class="text">
					研学人数：{{item.studentCount}}人
				</view>
				<view class="text">
					研学费用：{{item.totalPrice}}元
				</view>
			</view>
			<u-loadmore :status="loadmoreStatus" line />
		</view>
		<view class="empty" v-else>
			<u-empty mode="search" :iconSize="120" text="订单为空"></u-empty>
		</view>
	</view>
</template>

<script>
	import {
		orderTabs
	} from '@/utils/contant.js';

	export default {
		data() {
			return {
				currentTab: 0,
				orderTabs,
				page: 1,
				size: 10,
				list: [],
				loadmoreStatus: 'loadmore',
			}
		},
		onLoad(options) {
			this.currentTab = Number(options.currentTab) || this.currentTab;
			// this.getList();
		},
		onShow() {
			this.page = 1;
			this.list = [];
			this.getList();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.list = [];
			this.getList();
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			if(this.loadmoreStatus === 'nomore') return;
			this.loadmoreStatus = 'loading';
			this.page += 1;
			this.getList();
		},
		methods: {
			handleChangeTab(e) {
				this.currentTab = e.index;
				this.page = 1;
				this.list = [];
				this.loadmoreStatus = 'loadmore';
				this.getList();
			},
			async getList() {
				uni.showLoading({
					title: '加载中...',
				});
				const res = await this.$api('/study/mobile/personal/center/order/page', 'get', {
					page: this.page,
					limit: this.size,
					status: orderTabs[this.currentTab].value,
				});
				uni.hideLoading();

				this.loadmoreStatus = 'loadmore';
				this.list = this.list.concat(res.data.list);
				
				if (res.data.list.length < this.size) {
					this.loadmoreStatus = 'nomore';
					return;
				}
			},
			toOrderDetail(id) {
				uni.navigateTo({
					url: `/pages/my/order-detail?id=${id}`,
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.order {
		margin: 20rpx;

		.items {
			.item {
				margin-top: 30rpx;
				padding: 30rpx;
				background-color: #fff;
				border-radius: 20rpx;

				.title {
					padding-bottom: 20rpx;
					margin-bottom: 20rpx;
					color: #218BCC;
					border-bottom: 1rpx solid #ccc;
				}

				.text {
					padding: 4rpx 0;
					color: #203F3F;
				}
			}
		}
	}
</style>